package com.zhuannimei.ui;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.zhuannimei.GlobalApplication;
import com.zhuannimei.R;
import com.zhuannimei.adapter.OneShopAdapter;
import com.zhuannimei.adapter.ProductUserAdapter;
import com.zhuannimei.domain.OneShopProduct;
import com.zhuannimei.domain.ProductUser;
import com.zhuannimei.net.RequestServerData;
import com.zhuannimei.parser.RankParser;
import com.zhuannimei.service.AsyncDataLoader;
import com.zhuannimei.service.AsyncDataLoader.Callback;
import com.zhuannimei.utility.WuLog;
import com.zhuannimei.utility.ZnmHttpParams;
import com.zhuannimei.view.ZnmRefreshListView;
import com.zhuannimei.view.ZnmRefreshListView.OnCancelListener;
import com.zhuannimei.view.ZnmRefreshListView.OnRefreshListener;

/**
 * 积分榜
 * 
 * @author Jack 2014-3-6
 * 
 * 
 */
public class TabThreeActivity extends BaseActivity implements
		OnItemClickListener, OnCancelListener, OnRefreshListener {
	private ZnmRefreshListView znmRL;// 赚你妹下拉控件
	private static final String TAG = "TabThreeActivity";
	private ProductUserAdapter mAdapter;
	private List<ProductUser> mList = new ArrayList<ProductUser>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.fragment3);
		initViews();
		initEvents();
		setData();
	}

	private void setData() {
		new AsyncDataLoader(new Callback() {
			@Override
			public void onStart() {
				// 查询
				mList.clear();
				try {
					List<ProductUser> list = GlobalApplication.appDB
							.findAll(ProductUser.class);
					if (list != null && list.size() != 0) {
						mList.addAll(list);
					}
				} catch (Exception e) {
					WuLog.i(TAG, "查询异常了"+e);
				}
			}

			@Override
			public void onPrepare() {
				mAdapter = new ProductUserAdapter(getApplicationContext(),
						mList);
				znmRL.setAdapter(mAdapter);
				znmRL.onManualRefresh();
			}

			@Override
			public void onFinish() {
				getPointListFromNet();
			}
		}).execute();
	}

	@Override
	protected void onStart() {
		super.onStart();
		mTitlePane.setTitle("积分榜");
		mTitlePane.getLeftButton().setVisibility(View.VISIBLE);
		mTitlePane.getRightButton().setVisibility(View.VISIBLE);
		mTitlePane.setLeftButtonTextAndListener("返回", new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		mTitlePane.setRightButtonTextAndListener("帮助", new OnClickListener() {
			@Override
			public void onClick(View v) {
				showCustomToast("帮助说明");
			}
		});
	}

	private void getPointListFromNet() {
		RequestParams mParams = ZnmHttpParams.getDefaultRequestParams();
		mParams.addBodyParameter("pageIndex", "0");
		RequestServerData.userRank(mParams, new RequestCallBack<String>() {
			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				try {
					JSONObject jo = new JSONObject(responseInfo.result);
					if ("0".equals(jo.optString("code"))) {
						List<ProductUser> list = new RankParser()
								.parseJSON(responseInfo.result);
						if (list != null && list.size() != 0) {
							mList.clear();
							mList.addAll(list);
							WuLog.i(TAG, "list==");
						}
						mAdapter.notifyDataSetChanged();
						// 先删后存
						GlobalApplication.appDB.deleteByWhere(
								ProductUser.class, null);
						for (ProductUser pu : mList) {
							GlobalApplication.appDB.save(pu);
						}
						znmRL.onRefreshComplete();
					} else {
						showCustomToast("网络不给力");
					}
				} catch (JSONException e) {
					e.printStackTrace();
					showCustomToast("网络不给力");
				}
			}

			@Override
			public void onFailure(HttpException error, String msg) {
				showCustomToast("网络不给力");
			}
		});
	}

	protected void initViews() {
		WuLog.i(TAG, "initViews");
		znmRL = (ZnmRefreshListView) findViewById(R.id.point_me_ZR);
	}

	protected void initEvents() {
		znmRL.setOnItemClickListener(this);
		znmRL.setOnCancelListener(this);
		znmRL.setOnRefreshListener(this);
	}

	@Override
	public void onCancel() {
		znmRL.onRefreshComplete();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
	}

	@Override
	public void onRefresh() {
		getPointListFromNet();
	}

}
